import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {ProxyGetResponseList} from "./ProxyGetResponseList";
import {ProxyGetResponseRecord} from "./ProxyGetResponseRecord";
import {ProxySetResponseList} from "./ProxySetResponseList";
import {ProxySetThenGetResponseList} from "./ProxySetThenGetResponseList";
import {ProxyActionResponseList} from "./ProxyActionResponseList";
import {ProxyActionThenGetResponseList} from "./ProxyActionThenGetResponseList";
import {ProxyTransCommandResponse} from "./ProxyTransCommandResponse";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";

enum Choice {
    _ERR_NONE_SELECTED = -1,
    PROXY_GET_RESPONSE_LIST = 1,
    PROXY_GET_RESPONSE_RECORD = 2,
    PROXY_SET_RESPONSE_LIST = 3,
    PROXY_SET_THEN_GET_RESPONSE_LIST = 4,
    PROXY_ACTION_RESPONSE_LIST = 5,
    PROXY_ACTION_THEN_GET_RESPONSE_LIST = 6,
    PROXY_TRANS_COMMAND_RESPONSE = 7,
}

export class Proxy_Response implements AxdrType{

    dataCode : Buffer | null = null;
    choice: Choice = Choice._ERR_NONE_SELECTED;
    proxyGetResponseList : ProxyGetResponseList | null = null;
    proxyGetResponseRecord : ProxyGetResponseRecord | null = null;
    proxySetResponseList : ProxySetResponseList | null = null;
    proxySetThenGetResponseList : ProxySetThenGetResponseList | null = null;
    proxyActionResponseList : ProxyActionResponseList | null = null;
    proxyActionThenGetResponseList : ProxyActionThenGetResponseList | null = null;
    proxyTransCommandResponse : ProxyTransCommandResponse | null = null;

    constructor() {
    }

    set_dataCode(dataCode: Buffer){
        this.dataCode = dataCode;
    }

    setProxyGetResponseList(proxyGetResponseList : ProxyGetResponseList){
        this.resetChoices();
        this.choice = Choice.PROXY_SET_RESPONSE_LIST;
        this.proxyGetResponseList = proxyGetResponseList;
    }

    setProxyGetResponseRecord(proxyGetResponseRecord : ProxyGetResponseRecord){
        this.resetChoices();
        this.choice = Choice.PROXY_GET_RESPONSE_RECORD;
        this.proxyGetResponseRecord = proxyGetResponseRecord;
    }

    setProxySetResponseList(proxySetResponseList : ProxySetResponseList){
        this.resetChoices();
        this.choice = Choice.PROXY_SET_RESPONSE_LIST;
        this.proxySetResponseList = proxySetResponseList;
    }

    setProxySetThenGetResponseList(proxySetThenGetResponseList : ProxySetThenGetResponseList){
        this.resetChoices();
        this.choice = Choice.PROXY_SET_THEN_GET_RESPONSE_LIST;
        this.proxySetThenGetResponseList = proxySetThenGetResponseList;
    }

    setProxyActionResponseList(proxyActionResponseList : ProxyActionResponseList){
        this.resetChoices();
        this.choice = Choice.PROXY_ACTION_RESPONSE_LIST;
        this.proxyActionResponseList = proxyActionResponseList;
    }

    setProxyActionThenGetResponseList(proxyActionThenGetResponseList : ProxyActionThenGetResponseList){
        this.resetChoices();
        this.choice = Choice.PROXY_ACTION_THEN_GET_RESPONSE_LIST;
        this.proxyActionThenGetResponseList = proxyActionThenGetResponseList;
    }

    setProxyTransCommandResponse(proxyTransCommandResponse : ProxyTransCommandResponse){
        this.resetChoices();
        this.choice = Choice.PROXY_TRANS_COMMAND_RESPONSE;
        this.proxyTransCommandResponse = proxyTransCommandResponse;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.PROXY_GET_RESPONSE_LIST) {
            this.proxyGetResponseList = new ProxyGetResponseList();
            codeLength += this.proxyGetResponseList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_GET_RESPONSE_RECORD) {
            this.proxyGetResponseRecord = new ProxyGetResponseRecord();
            codeLength += this.proxyGetResponseRecord.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_SET_RESPONSE_LIST) {
            this.proxySetResponseList = new ProxySetResponseList();
            codeLength += this.proxySetResponseList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_SET_THEN_GET_RESPONSE_LIST) {
            this.proxySetThenGetResponseList = new ProxySetThenGetResponseList();
            codeLength += this.proxySetThenGetResponseList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_ACTION_RESPONSE_LIST) {
            this.proxyActionResponseList = new ProxyActionResponseList();
            codeLength += this.proxyActionResponseList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_ACTION_THEN_GET_RESPONSE_LIST) {
            this.proxyActionThenGetResponseList = new ProxyActionThenGetResponseList();
            codeLength += this.proxyActionThenGetResponseList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.PROXY_TRANS_COMMAND_RESPONSE) {
            this.proxyTransCommandResponse = new ProxyTransCommandResponse();
            codeLength += this.proxyTransCommandResponse.decode(input);
            return codeLength;
        }

        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.PROXY_TRANS_COMMAND_RESPONSE) {
            codeLength += this.proxyTransCommandResponse.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(7);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_ACTION_THEN_GET_RESPONSE_LIST) {
            codeLength += this.proxyActionThenGetResponseList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(6);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_ACTION_RESPONSE_LIST) {
            codeLength += this.proxyActionResponseList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(5);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_SET_THEN_GET_RESPONSE_LIST) {
            codeLength += this.proxySetThenGetResponseList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(4);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_SET_RESPONSE_LIST) {
            codeLength += this.proxySetResponseList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(3);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_GET_RESPONSE_RECORD) {
            codeLength += this.proxyGetResponseRecord.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(2);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.PROXY_GET_RESPONSE_LIST) {
            codeLength += this.proxyGetResponseList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(1);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.proxyGetResponseList = null;
        this.proxyGetResponseRecord = null;
        this.proxySetResponseList = null
        this.proxySetThenGetResponseList = null;
        this.proxyActionResponseList = null;
        this.proxyActionThenGetResponseList = null;
        this.proxyTransCommandResponse = null;
    }

    toString(): string {
        if (this.choice == Choice.PROXY_GET_RESPONSE_LIST) {
            if (this.proxyGetResponseList != null) {
                return "choice: {proxyGetResponseList: " + this.proxyGetResponseList + "}";
            } else {
                return "choice is proxyGetResponseList but proxyGetResponseList is null";
            }
        }else if(this.choice == Choice.PROXY_GET_RESPONSE_RECORD){
            if(this.proxyGetResponseRecord != null){
                return "choice: {proxyGetResponseRecord: " + this.proxyGetResponseRecord + "}";
            }else{
                return "choice is proxyGetResponseRecord but proxyGetResponseRecord is null";
            }
        }else if(this.choice == Choice.PROXY_SET_RESPONSE_LIST){
            if(this.proxySetResponseList != null){
                return "choice: {proxySetResponseList: " + this.proxySetResponseList + "}";
            }else{
                return "choice is proxySetResponseList but proxySetResponseList is null";
            }
        }else if(this.choice == Choice.PROXY_SET_THEN_GET_RESPONSE_LIST){
            if(this.proxySetThenGetResponseList != null){
                return "choice: {proxySetThenGetResponseList: " + this.proxySetThenGetResponseList + "}";
            }else{
                return "choice is proxySetThenGetResponseList but proxySetThenGetResponseList is null";
            }
        }else if(this.choice == Choice.PROXY_ACTION_RESPONSE_LIST){
            if(this.proxyActionResponseList != null){
                return "choice: {proxyActionResponseList: " + this.proxyActionResponseList + "}";
            }else{
                return "choice is proxyActionResponseList but proxyActionResponseList is null";
            }
        } else if(this.choice == Choice.PROXY_ACTION_THEN_GET_RESPONSE_LIST){
            if(this.proxyActionThenGetResponseList != null){
                return "choice: {proxyActionThenGetResponseList: " + this.proxyActionThenGetResponseList + "}";
            }else{
                return "choice is proxyActionThenGetResponseList but proxyActionThenGetResponseList is null";
            }
        }else if(this.choice == Choice.PROXY_TRANS_COMMAND_RESPONSE){
            if(this.proxyTransCommandResponse != null){
                return "choice: {proxyTransCommandResponse: " + this.proxyTransCommandResponse + "}";
            }else{
                return "choice is proxyTransCommandResponse but proxyTransCommandResponse is null";
            }
        }else {
            return "unknown";
        }
    }

}
